package com.esd.controller.csair;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.esd.csair.Csair;
import com.esd.csair.CsairImpl;

@Controller
@RequestMapping(value = "cs")
public class CsAccessorialService {

	private static Logger log = Logger.getLogger(CsAccessorialService.class);
	/**
	 * 航班动态跳转页面
	 * @return
	 */
	@RequestMapping(value = "flightDynamicsPage", method = RequestMethod.GET)
	public String flightDynamics() {
		log.info("====================航班动态=====================");
		return ("csair/flightDynamics");
	}
	
	/**
	 * 航班动态按出发、到达地点查询
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "flightDynamicsPlace", method = RequestMethod.GET)
	public ModelAndView flightDynamicsPlace(HttpSession session, HttpServletRequest request) {
		log.info("===================按出发、到达地点查询加载航班动态页==================");
		String c1 = request.getParameter("c1");
		String c2 = request.getParameter("c2");
		String date = request.getParameter("d");
		int year = Integer.valueOf(request.getParameter("year"));
		int month = Integer.valueOf(request.getParameter("month"));
		int day = Integer.valueOf(request.getParameter("day"));
		Map<String, Object> map = new HashMap<String, Object>();
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.flightDynamicsPlace(c1, c2, date,year,month,day);
		return new ModelAndView("csair/flightDynamicsMX", map);
	}
	
	/**
	 * 航班动态按照航班号查询
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "flightDynamicsNum", method = RequestMethod.GET)
	public ModelAndView flightDynamicsNum(HttpSession session, HttpServletRequest request) {
		log.info("===================按航班号查询加载航班动态页==================");
		String flightNumber = request.getParameter("f");
		String date = request.getParameter("d");
		int year = Integer.valueOf(request.getParameter("year"));
		int month = Integer.valueOf(request.getParameter("month"));
		int day = Integer.valueOf(request.getParameter("day"));
		Map<String, Object> map = new HashMap<String, Object>();
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webClient.flightDynamicsNum(flightNumber, date,year,month,day);
		return new ModelAndView("csair/flightDynamicsMX", map);
	}
	
	
	/**
	 * 退改票
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "refundTicket", method = RequestMethod.GET)
	public ModelAndView refundTicket() {
		log.info("=======================退改票===========================");
		return new ModelAndView("csair/refundTicket");
	}
	
	/**
	 * 根据状态查询购票记录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "findOrderByStatus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findOrderByStatus(HttpServletRequest request, HttpSession session) {
		log.info("=======================根据状态查询购票记录==============================");
		String status = request.getParameter("status");
		String pageNum = request.getParameter("pageNum");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		Map<String,Object> map = new HashMap<>();
		map = webClient.findOrderByStatus(status,pageNum);
		return map;
	}
	
	
	/**
	 * 默认查询变更记录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "findChangeList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findChangeList(HttpServletRequest request, HttpSession session) {
		log.info("=======================默认查询变更记录==============================");
		String pageNum = request.getParameter("page");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		Map<String,Object> map = new HashMap<String,Object>();
		map = webClient.findChangeList(pageNum);
		return map;

	}
	
	/**
	 * 根据状态查询变更记录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "findChangeListByStatus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findChangeListByStatus(HttpServletRequest request, HttpSession session) {
		log.info("=======================根据状态查询变更记录==============================");
		String status = request.getParameter("status");
		String pageNum = request.getParameter("pageNum");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		Map<String,Object> map = new HashMap<String,Object>();
		map = webClient.findChangeListByStatus(status,pageNum);
		return map;
	}
	
	
	/**
	 * 默认查询退票记录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "findRefundList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findRefundList(HttpServletRequest request, HttpSession session) {
		log.info("=======================默认查询退票记录==============================");
		String pageNum = request.getParameter("page");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		Map<String,Object> map = new HashMap<String,Object>();
		map = webClient.findRefundList(pageNum);
		return map;
		
	}
	
	
	/**
	 * 根据状态查询退票记录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "findRefundListByStatus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findRefundListByStatus(HttpServletRequest request, HttpSession session) {
		log.info("=======================根据状态查询变更记录==============================");
		String status = request.getParameter("status");
		String pageNum = request.getParameter("pageNum");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		Map<String,Object> map = new HashMap<String,Object>();
		map = webClient.findRefundListByStatus(status,pageNum);
		return map;
	}
	
	/**
	 * 全站搜索
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "csairSearch", method = RequestMethod.GET)
	public ModelAndView csairSearch() {
		log.info("======================全站搜索=============================");
		return new ModelAndView("csair/csairSearch");
	}
	/**
	 * 加载全站搜索
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "loadCsairSearch", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loadCsairSearch(HttpSession session,HttpServletRequest request) {
		log.info("======================加载全站搜索=============================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String href = request.getParameter("href");
		Csair webclient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webclient.loadCsairSearch(href);
		return map;
	}

	/**
	 * 全站搜索提交
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "csairSearchAll", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> csairSearchAll(HttpSession session,HttpServletRequest request){
		log.info("======================全站搜索提交===========================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String keyWord = request.getParameter("keyWord");
		try {
			keyWord = URLDecoder.decode(keyWord,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Csair webclient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webclient.csairSearchAll(keyWord);
		return map;
	}
	
	/**
	 * 全站搜索页数点击
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "searchPageClick", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> searchPageClick(HttpSession session,HttpServletRequest request){
		log.info("======================全站搜索页数点击===========================");
		Map<String, Object> map = new HashMap<String ,Object>();
		String pageCount = request.getParameter("pageCount");
		System.out.println("pageCount=========>"+pageCount);
		try {
			pageCount = URLDecoder.decode(pageCount,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Csair webclient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		map = webclient.searchPageClick(pageCount);
		return map;
	}
	
	
	
}
